-- EXEC SP_CREATE_UPDATE_WAFER_DETAILS
ALTER PROC SP_CREATE_UPDATE_WAFER_DETAILS
(
	@PART_NUMBER VARCHAR(50),
	@SHORT_NAME VARCHAR(50),
	@PLANNER_CODE VARCHAR(50),
	@PRODUCT_FUNCTION VARCHAR(200),
	@PROCESS_GEOMETRY VARCHAR(50),
	@PRODUCT_STATUS VARCHAR(50),
	@CHIPSIZEX VARCHAR(50),
	@CHIPSIZEY VARCHAR(50),
	@WaferTHICKNESS VARCHAR(50),
	@DIESIZE VARCHAR(50),
	@FABCYCLETIME VARCHAR(50),
	@MASKSERVICETYPE VARCHAR(50),
	@PROCESS VARCHAR(50),
	@WAFERSIZE VARCHAR(50),
	@METALLAYERS VARCHAR(50),
	@POLYLAYERS VARCHAR(50),
	@EPITAXIAL VARCHAR(50),
	@NOOFMASKLAYERS VARCHAR(50),
	@FABCOUNTRY VARCHAR(50),
	@OPERATINGVOLTAGE VARCHAR(50),
	@PRODUCTTYPE VARCHAR(50),
	@IMPLANTCAPACITOR VARCHAR(50),
	@QTR_STDCOST1 NUMERIC(18,4),
	@QTR_STDCOST2 NUMERIC(18,4),
	@QTR_STDCOST3 NUMERIC(18,4),	
	@QTR_STDCOST4 NUMERIC(18,4),
	@GDPW INT
)
AS
BEGIN
	DECLARE @PROCESSSTEP_ID INT
	DECLARE @ROWCOUNT INT
	DECLARE @CURRENT_QUARTER INT
	DECLARE @STDCOST1 NUMERIC(18,4)
	DECLARE @STDCOST2 NUMERIC(18,4)
	DECLARE @STDCOST3 NUMERIC(18,4)
	DECLARE @STDCOST4 NUMERIC(18,4)
	declare @temp_ps_id int
	declare @temp_ps_id1 int
	DECLARE @YEAR INT
	DECLARE @YEAR1 INT
	SELECT @PROCESSSTEP_ID = P.PROCESSSTEP_ID FROM PRODUCT P, PROCESS_STEP PS WHERE P.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS.PS_TYPE = 'FAB' AND PART_NUMBER = @PART_NUMBER

	SELECT @CURRENT_QUARTER = CQC_QUARTER_NO FROM Company_Quarterly_Calendar WHERE CONVERT( VARCHAR, CURRENT_TIMESTAMP ) >= CQC_START_DATE AND CONVERT( VARCHAR, CURRENT_TIMESTAMP ) <= CQC_END_DATE

	IF @CURRENT_QUARTER = 4
	BEGIN
		SET @YEAR = YEAR(CURRENT_TIMESTAMP) 
	    Select @temp_ps_id = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR
	
		if @temp_ps_id is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q2_Standard_Cost, Q3_Standard_Cost, Q4_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST1, @QTR_STDCOST2, @QTR_STDCOST3, @QTR_STDCOST3, '01/01/' + CONVERT(VARCHAR,@YEAR), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set 
			Q1_Standard_Cost = @QTR_STDCOST1,Q2_Standard_Cost = @QTR_STDCOST2,Q3_Standard_Cost = @QTR_STDCOST3,	Q4_Standard_Cost = @QTR_STDCOST4,
			Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR
		END
	
	END
	ELSE IF @CURRENT_QUARTER = 3
	BEGIN
		SET @YEAR = YEAR(CURRENT_TIMESTAMP) 
		SET @YEAR1 = YEAR(CURRENT_TIMESTAMP)-1  
		Select @temp_ps_id = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR
		Select @temp_ps_id1 = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR1
		if @temp_ps_id is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q2_Standard_Cost, Q3_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST2, @QTR_STDCOST3, @QTR_STDCOST4, '01/01/' + CONVERT(VARCHAR,@YEAR), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Q1_Standard_Cost = @QTR_STDCOST2,Q2_Standard_Cost = @QTR_STDCOST3,Q3_Standard_Cost = @QTR_STDCOST4,
			Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR
		END	

		if @temp_ps_id1 is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q4_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST1, '01/01/' + CONVERT(VARCHAR,@YEAR1), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Q4_Standard_Cost = @QTR_STDCOST1, Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR1
		END	
			
	END
	ELSE IF @CURRENT_QUARTER = 2
	BEGIN
		SET @YEAR = YEAR(CURRENT_TIMESTAMP) 
		SET @YEAR1 = YEAR(CURRENT_TIMESTAMP)-1  
		Select @temp_ps_id = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR
		Select @temp_ps_id1 = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR1
		if @temp_ps_id is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Q2_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST3, @QTR_STDCOST4, '01/01/' + CONVERT(VARCHAR,@YEAR), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Q1_Standard_Cost = @QTR_STDCOST3,Q2_Standard_Cost = @QTR_STDCOST4, Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR
		END	

		if @temp_ps_id1 is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q3_Standard_Cost, Q4_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST1, @QTR_STDCOST2, '01/01/' + CONVERT(VARCHAR,@YEAR1), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Q3_Standard_Cost = @QTR_STDCOST1,Q4_Standard_Cost = @QTR_STDCOST2, Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR1
		END	
	END
	ELSE
	BEGIN
		SET @YEAR = YEAR(CURRENT_TIMESTAMP) 
		SET @YEAR1 = YEAR(CURRENT_TIMESTAMP)-1  
		Select @temp_ps_id = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR
		Select @temp_ps_id1 = ProcessStep_Id from update_view_cost_table where ProcessStep_ID = @PROCESSSTEP_ID and Year(Fin_Year) = @YEAR1
		if @temp_ps_id is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q1_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST4, '01/01/' + CONVERT(VARCHAR,@YEAR), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Q1_Standard_Cost = @QTR_STDCOST4,Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR
		END	

		if @temp_ps_id1 is null
		begin 
			INSERT INTO Update_View_Cost_Table ( ProcessStep_id, Q2_Standard_Cost, Q3_Standard_Cost, Q4_Standard_Cost, Fin_Year, Timestamp)
				VALUES(@PROCESSSTEP_ID, @QTR_STDCOST1, @QTR_STDCOST2,@QTR_STDCOST3, '01/01/' + CONVERT(VARCHAR,@YEAR1), CURRENT_TIMESTAMP)	
		END
		ELSE
		BEGIN
			UPDATE Update_View_Cost_Table set Q2_Standard_Cost = @QTR_STDCOST1, Q3_Standard_Cost = @QTR_STDCOST2,Q4_Standard_Cost = @QTR_STDCOST3, Timestamp = CURRENT_TIMESTAMP where ProcessStep_id = @PROCESSSTEP_ID and year(Fin_Year) = @YEAR1
		END		
	END
	
	
	IF @PROCESSSTEP_ID IS NOT NULL AND @PROCESSSTEP_ID > 0
	BEGIN

		
		IF @SHORT_NAME IS NOT NULL AND LEN(LTRIM(RTRIM(@SHORT_NAME)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Short Name'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Short Name', 'TEXT', @SHORT_NAME )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @SHORT_NAME where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Short Name'
			END
		END

		IF @PLANNER_CODE IS NOT NULL AND LEN(LTRIM(RTRIM(@PLANNER_CODE)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Planner Code'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Planner Code', 'TEXT', @PLANNER_CODE )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @PLANNER_CODE where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Planner Code'
			END
		END

		IF @PRODUCT_FUNCTION IS NOT NULL AND LEN(LTRIM(RTRIM(@PRODUCT_FUNCTION)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Product function'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Product function', 'TEXT', @PRODUCT_FUNCTION )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @PRODUCT_FUNCTION where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Product function'
			END
		END


		IF @PROCESS_GEOMETRY IS NOT NULL AND LEN(LTRIM(RTRIM(@PROCESS_GEOMETRY)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Process Geometry'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Process Geometry', 'TEXT', @PROCESS_GEOMETRY )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @PROCESS_GEOMETRY where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Process Geometry'
			END
		END

		IF @PRODUCT_STATUS IS NOT NULL AND LEN(LTRIM(RTRIM(@PRODUCT_STATUS)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Product Status'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Product Status', 'TEXT', @PRODUCT_STATUS )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @PRODUCT_STATUS where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Product Status'
			END
		END

		IF @CHIPSIZEX IS NOT NULL AND LEN(LTRIM(RTRIM(@CHIPSIZEX)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Chip Size X'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Chip Size X', 'TEXT', @CHIPSIZEX )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @CHIPSIZEX where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Chip Size X'
			END
		END

		IF @CHIPSIZEY IS NOT NULL AND LEN(LTRIM(RTRIM(@CHIPSIZEY)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Chip Size Y'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Chip Size Y', 'TEXT', @CHIPSIZEY )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @CHIPSIZEY where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Chip Size Y'
			END
		END

		IF @WaferTHICKNESS IS NOT NULL AND LEN(LTRIM(RTRIM(@WaferTHICKNESS)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Wafer thickness'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Wafer thickness', 'TEXT', @WaferTHICKNESS )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @WaferTHICKNESS where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Wafer thickness'
			END
		END
		IF @DIESIZE IS NOT NULL AND LEN(LTRIM(RTRIM(@DIESIZE)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Die Size'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Die Size', 'TEXT', @DIESIZE )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @DIESIZE where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Die Size'
			END
		END

		IF @FABCYCLETIME IS NOT NULL AND LEN(LTRIM(RTRIM(@FABCYCLETIME)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Standard Cycle Time'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Standard Cycle Time', 'TEXT', @FABCYCLETIME )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @FABCYCLETIME where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Standard Cycle Time'
			END
		END

		IF @MASKSERVICETYPE IS NOT NULL AND LEN(LTRIM(RTRIM(@MASKSERVICETYPE)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Mask Service Type'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Mask Service Type', 'TEXT', @MASKSERVICETYPE )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @MASKSERVICETYPE where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Mask Service Type'
			END
		END

		IF @PROCESS IS NOT NULL AND LEN(LTRIM(RTRIM(@PROCESS)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Fab Process'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Fab Process', 'TEXT', @PROCESS )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @PROCESS where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Fab Process'
			END
		END
		IF @WAFERSIZE IS NOT NULL AND LEN(LTRIM(RTRIM(@WAFERSIZE)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Wafer Size'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Wafer Size', 'TEXT', @WAFERSIZE )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @WAFERSIZE where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Wafer Size'
			END
		END

		IF @METALLAYERS IS NOT NULL AND LEN(LTRIM(RTRIM(@METALLAYERS)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Metal Layers'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Metal Layers', 'TEXT', @METALLAYERS )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @METALLAYERS where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Metal Layers'
			END
		END

		IF @POLYLAYERS IS NOT NULL AND LEN(LTRIM(RTRIM(@POLYLAYERS)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Poly Layers'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Poly Layers', 'TEXT', @POLYLAYERS )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @POLYLAYERS where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Poly Layers'
			END
		END

		IF @EPITAXIAL IS NOT NULL AND LEN(LTRIM(RTRIM(@EPITAXIAL)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Epitaxial'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Epitaxial', 'TEXT', @EPITAXIAL )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @EPITAXIAL where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Epitaxial'
			END
		END

		IF @NOOFMASKLAYERS IS NOT NULL AND LEN(LTRIM(RTRIM(@NOOFMASKLAYERS)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = '# of Mask Layers'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, '# of Mask Layers', 'TEXT', @NOOFMASKLAYERS )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @NOOFMASKLAYERS where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = '# of Mask Layers'
			END
		END

		IF @FABCOUNTRY IS NOT NULL AND LEN(LTRIM(RTRIM(@FABCOUNTRY)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Fab Country'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Fab Country', 'TEXT', @FABCOUNTRY )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @FABCOUNTRY where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Fab Country'
			END
		END

		IF @OPERATINGVOLTAGE IS NOT NULL AND LEN(LTRIM(RTRIM(@OPERATINGVOLTAGE)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Operating voltage'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Operating voltage', 'TEXT', @OPERATINGVOLTAGE )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @OPERATINGVOLTAGE where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Operating voltage'
			END
		END

		IF @PRODUCTTYPE IS NOT NULL AND LEN(LTRIM(RTRIM(@PRODUCTTYPE)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Product type'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Product type', 'TEXT', @PRODUCTTYPE )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @PRODUCTTYPE where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Product type'
			END
		END

		IF @IMPLANTCAPACITOR IS NOT NULL AND LEN(LTRIM(RTRIM(@IMPLANTCAPACITOR)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Implant capacitor'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Implant capacitor', 'TEXT', @IMPLANTCAPACITOR )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @IMPLANTCAPACITOR where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Implant capacitor'
			END
		END

		IF @GDPW IS NOT NULL AND LEN(LTRIM(RTRIM(@GDPW)))>0
		BEGIN
			SELECT @ROWCOUNT = COUNT(*) FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Gross Die/Wafer'
			IF  @ROWCOUNT < 1
			BEGIN
				INSERT INTO PROCESS_PARAMETER VALUES ( @PROCESSSTEP_ID, 'Gross Die/Wafer', 'TEXT', @GDPW )
			END
			ELSE
			BEGIN
				Update PROCESS_PARAMETER set parameter_Value = @GDPW where PROCESSSTEP_ID = @PROCESSSTEP_ID AND PARAMETER_KEY = 'Gross Die/Wafer'
			END
		END
	END
END